﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using fsw.Common;
using System.Text;

namespace fsw.Web.Manager
{
    public partial class LogCard : Web.UI.ManagePage
    {
        Model.Admin model_admin;
        protected int totalCount;
        protected int _Pid = 0;//项目id
        protected int page;
        protected int pageSize;
        protected string keywords = string.Empty;
        protected string PageContent = string.Empty;
        protected string Path_File = "/" + DTKeys.Path_File_No;
        protected DataTable rptList;
        protected List<Model.Project> List_Project = new List<Model.Project>();//
        protected string List_Pro_Str = string.Empty;
        protected string List_User_Str = string.Empty;
        protected int _UserId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            model_admin = GetAdminInfo();
            if (model_admin.TypeId > 1) Response.End();
            this.keywords = DTRequest.GetQueryString("keywords");
            this.pageSize = Utils.GetPageSize(24, "Manager_logcard_page_size");
            this.page = DTRequest.GetQueryInt("page", 1);
            this._Pid = DTRequest.GetQueryInt("pid");
            this._UserId = DTRequest.GetQueryInt("userid");
            if (DTRequest.IsPost())
            {
                UpdateBind();
                Response.End();
            }
            if (!Page.IsPostBack)
            {
                RptBind("a.id > 0 and a.isStatus = 1" + CombSqlTxt(), "a.Id desc");
                RptProject(); RptUser();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_strWhere"></param>
        /// <param name="_orderby"></param>
        private void RptBind(string _strWhere, string _orderby)
        {
            BLL.Admin_Pic bll = new BLL.Admin_Pic();
            rptList = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount).Tables[0];
            string pageUrl = Utils.CombUrlTxt("LogCard.aspx", "pid={0}&userid={1}&keywords={2}&page={3}", this._Pid.ToString(), this._UserId.ToString(), this.keywords, "__id__");
            this.PageContent = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        /// <summary>
        /// 数据更新
        /// </summary>
        private void UpdateBind()
        {
            BLL.Admin_Pic bll = new BLL.Admin_Pic();
            DataTable _dt = bll.GetListTable("", "");
            if (_dt.Rows.Count > 0)
            {
                foreach (DataRow m in _dt.Rows)
                {
                    if (!DirFileHelper.IsExists_File(Path_File + m["PicPath"] + "/" + m["PicName"]))
                    {
                        bll.UpdateField(int.Parse(m["Id"].ToString()), "isStatus = 0");
                    }
                    //else
                    //{
                    //    bll.UpdateField(int.Parse(m["Id"].ToString()), "isStatus = 1");
                    //}
                }
            }
            Response.Write(JsonErr("success", "更新成功!"));
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void RptProject()
        {
            List_Project = new BLL.Project().GetList();
            StringBuilder Sb = new StringBuilder();
            Sb.Append("<option value=\"\">请选择项目</option>");
            foreach (var m in List_Project)
            {
                string _selected = m.Pid == this._Pid ? "selected=\"selected\"" : "";
                Sb.AppendFormat("<option value=\"{0}\"{2}>{1}</option>", m.Pid.ToString(), m.HosName, _selected);
            }
            List_Pro_Str = Sb.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string CombSqlTxt()
        {
            StringBuilder strTemp = new StringBuilder();
            if (this._Pid > 0) strTemp.Append(" and a.Pid=" + this._Pid.ToString());
            if (this._UserId > 0) strTemp.Append(" and a.UserId=" + this._UserId.ToString());
            if (!string.IsNullOrEmpty(this.keywords)) strTemp.Append(" and (a.PicPath like '%" + this.keywords + "%' or a.PicName like '%" + this.keywords + "%')");
            return strTemp.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        private void RptUser()
        {
            List<Model.Admin> _list = new List<Model.Admin>();
            StringBuilder strTemp = new StringBuilder();
            BLL.Admin bll = new BLL.Admin();
            _list = bll.GetList(100, " exists(select UserId from Y_Admin_Pic where UserId = a.Id) ", " a.UserName asc");
            strTemp.Append("<option value=\"\">请选择用户</option>");
            foreach (var m in _list)
            {
                string _selected = m.Id == this._UserId ? "selected=\"selected\"" : "";
                strTemp.AppendFormat("<option value=\"{0}\"{2}>{1}</option>", m.Id, m.UserName + "(" + m.RealName + ")", _selected);
            }
            List_User_Str = strTemp.ToString();
        }
    }
}